package registrazione;


import java.io.IOException;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entityBean.Agenzia;

import sessionBean.RegistrazioneCtrlRemote;

/**
 * Servlet implementation class RegAgenziaServlet
 */
public class RegAgenziaServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegAgenziaServlet() {

		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			processaRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			processaRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private RegistrazioneCtrlRemote getRegistrazioneCtrl() throws NamingException {

		Properties p = new Properties();
		p.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
		p.setProperty("java.naming.provider.url", "localhost:1099");
		p.setProperty("java.naming.url.pkgs", "org.jboss.naming");

		Object obj = new InitialContext(p).lookup("RegistrazioneCtrl/remote");

		return (RegistrazioneCtrlRemote)PortableRemoteObject.narrow(obj, RegistrazioneCtrlRemote.class);

	}

	public void processaRichiesta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException {

		RegistrazioneCtrlRemote reg = getRegistrazioneCtrl();
		
		String piva = request.getParameter("piva");
		String nome = request.getParameter("nome");
		String indirizzo = request.getParameter("indirizzo");
		String città = request.getParameter("città");
		String tel = request.getParameter("telefono");
		String email = request.getParameter("email");
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		//Verifico che siano tutti compilati
		// ...
		
		Agenzia a = new Agenzia();
		
		a.setPiva(piva);
		a.setNome(nome);
		a.setIndirizzo(indirizzo);
		a.setCittà(città);
		a.setTelefono(tel);
		a.setEmail(email);
		a.setUsername(username);
		a.setPassword(password);

		int ris = reg.regAgenzia(a);

		if(ris == 0) {

			//redirect Agenzia.jsp

		}

		else {

			//redirect con messaggio di errore a RegAgenzia.jsp

		}

	}

}
